package com.gl.handler;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>Description:全局拦截器</p>
 *
 * @author gaol
 * @version 1.0
 * @date 2017/5/16
 */
@Component
public class CustomInterceptor extends HandlerInterceptorAdapter {


    /*请求前*/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*此处可以做权限拦截 根据访问地址或者根据注解RequestMapping的value值做URL的动态拦截如下*/
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        String[] value = handlerMethod.getMethod().getAnnotation(RequestMapping.class).value();
        for (String str : value) {
            System.out.println("拦截到的方法：" + str);
        }
        return true;
    }

    /*请求结束 没有返回视图前*/
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    /*请求完全结束*/
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    /*异步请求。。。*/
    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }
}
